home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / include / scribus-ng / fparser.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-01-22  |  4.7 KB  |  164 lines

  1. /*
  2. For general Scribus (>=1.3.2) copyright and licensing information please refer
  3. to the COPYING file provided with the program. Following this notice may exist
  4. a copyright and/or license notice that predates the release of Scribus 1.3.2
  5. for which a new license (GPL+exception) is in place.
  6. */
  7. /***************************************************************************\
  8. |* Function parser v2.8 by Warp                                            *|
  9. |* ----------------------------                                            *|
  10. |* Parses and evaluates the given function with the given variable values. *|
  11. |* See fparser.txt for details.                                            *|
  12. |*                                                                         *|
  13. \***************************************************************************/
  14.  
  15. #ifndef ONCE_FPARSER_H_
  16. #define ONCE_FPARSER_H_
  17.  
  18. #include <string>
  19. #include <map>
  20. #include <vector>
  21.  
  22. #ifdef FUNCTIONPARSER_SUPPORT_DEBUG_OUTPUT
  23. #include <iostream>
  24. #endif
  25.  
  26. class FunctionParser
  27. {
  28. public:
  29.     enum ParseErrorType
  30.     {
  31.         SYNTAX_ERROR=0, MISM_PARENTH, MISSING_PARENTH, EMPTY_PARENTH,
  32.         EXPECT_OPERATOR, OUT_OF_MEMORY, UNEXPECTED_ERROR, INVALID_VARS,
  33.         ILL_PARAMS_AMOUNT, PREMATURE_EOS, EXPECT_PARENTH_FUNC,
  34.         FP_NO_ERROR
  35.     };
  36.  
  37.  
  38.     int Parse(const std::string& Function, const std::string& Vars,
  39.               bool useDegrees = false);
  40.     const char* ErrorMsg() const;
  41.     inline ParseErrorType GetParseErrorType() const { return parseErrorType; }
  42.  
  43.     double Eval(const double* Vars);
  44.     inline int EvalError() const { return evalErrorType; }
  45.  
  46.     bool AddConstant(const std::string& name, double value);
  47.  
  48.     typedef double (*FunctionPtr)(const double*);
  49.  
  50.     bool AddFunction(const std::string& name,
  51.                      FunctionPtr, unsigned paramsAmount);
  52.     bool AddFunction(const std::string& name, FunctionParser&);
  53.  
  54.     void Optimize();
  55.  
  56.  
  57.     FunctionParser();
  58.     ~FunctionParser();
  59.  
  60.     // Copy constructor and assignment operator (implemented using the
  61.     // copy-on-write technique for efficiency):
  62.     FunctionParser(const FunctionParser&);
  63.     FunctionParser& operator=(const FunctionParser&);
  64.  
  65.  
  66. #ifdef FUNCTIONPARSER_SUPPORT_DEBUG_OUTPUT
  67.     // For debugging purposes only:
  68.     void PrintByteCode(std::ostream& dest) const;
  69. #endif
  70.  
  71.  
  72.  
  73. //========================================================================
  74. private:
  75. //========================================================================
  76.  
  77. // Private data:
  78. // ------------
  79.     ParseErrorType parseErrorType;
  80.     int evalErrorType;
  81.  
  82.     struct Data
  83.     {
  84.         unsigned referenceCounter;
  85.  
  86.         int varAmount;
  87.         bool useDegreeConversion;
  88.  
  89.         typedef std::map<std::string, unsigned> VarMap_t;
  90.         VarMap_t Variables;
  91.  
  92.         typedef std::map<std::string, double> ConstMap_t;
  93.         ConstMap_t Constants;
  94.  
  95.         VarMap_t FuncPtrNames;
  96.         struct FuncPtrData
  97.         {
  98.             FunctionPtr ptr; unsigned params;
  99.             FuncPtrData(FunctionPtr p, unsigned par): ptr(p), params(par) {}
  100.         };
  101.         std::vector<FuncPtrData> FuncPtrs;
  102.  
  103.         VarMap_t FuncParserNames;
  104.         std::vector<FunctionParser*> FuncParsers;
  105.  
  106.         unsigned* ByteCode;
  107.         unsigned ByteCodeSize;
  108.         double* Immed;
  109.         unsigned ImmedSize;
  110.         double* Stack;
  111.         unsigned StackSize;
  112.  
  113.         Data();
  114.         ~Data();
  115.         Data(const Data&);
  116.  
  117.         Data& operator=(const Data&); // not implemented on purpose
  118.     };
  119.  
  120.     Data* data;
  121.     unsigned evalRecursionLevel;
  122.  
  123.     // Temp data needed in Compile():
  124.     unsigned StackPtr;
  125.     std::vector<unsigned>* tempByteCode;
  126.     std::vector<double>* tempImmed;
  127.  
  128.  
  129. // Private methods:
  130. // ---------------
  131.     void copyOnWrite();
  132.  
  133.  
  134.     bool checkRecursiveLinking(const FunctionParser*) const;
  135.  
  136.     bool isValidName(const std::string&) const;
  137.     Data::VarMap_t::const_iterator FindVariable(const char*,
  138.                                                 const Data::VarMap_t&) const;
  139.     Data::ConstMap_t::const_iterator FindConstant(const char*) const;
  140.     int CheckSyntax(const char*);
  141.     bool Compile(const char*);
  142.     bool IsVariable(int);
  143.     void AddCompiledByte(unsigned);
  144.     void AddImmediate(double);
  145.     void AddFunctionOpcode(unsigned);
  146.     inline void incStackPtr();
  147.     int CompileIf(const char*, int);
  148.     int CompileFunctionParams(const char*, int, unsigned);
  149.     int CompileElement(const char*, int);
  150.     int CompilePow(const char*, int);
  151.     int CompileUnaryMinus(const char*, int);
  152.     int CompileMult(const char*, int);
  153.     int CompileAddition(const char*, int);
  154.     int CompileComparison(const char*, int);
  155.     int CompileAnd(const char*, int);
  156.     int CompileOr(const char*, int);
  157.     int CompileExpression(const char*, int, bool=false);
  158.  
  159.  
  160.     void MakeTree(void*) const;
  161. };
  162.  
  163. #endif
  164.